
#######################################
#Relative Capture: Quasi-Experimental Evidence from the Chinese Judiciary#
#Matching using R#
#Originally Created on December 11, 2014#
#Replication File Created on March 21, 2017#
######################################

# install.packages("rgenoud")
# install.packages("MASS")
# install.packages("Matching")
# install.packages("foreign")
# install.packages("gmodels")
# install.packages("rbounds")

library(rgenoud)
library(MASS)
library(Matching)
library(foreign)
library(gmodels)
library(rbounds)

######################################
#SOEs#
######################################

soebasic <- read.dta("/Users/ywang/Documents/My Documents/Research/working papers/political connections and courts/data/working/CPS capture datafile/matching/SOEmatch.dta")

attach(soebasic)
names(soebasic)

Y<-win
Tr<-treat


#Genetic Matching

X<-cbind(A_ASSETS, A_AGE, contractdispute, home, stake_log, provincecode)

BalanceMatrix<-cbind(A_ASSETS, A_AGE, contractdispute, home, stake_log, provincecode, I(A_ASSETS^2), I(A_AGE^2), I(A_ASSETS*A_AGE), I(A_ASSETS*home),I(A_AGE*home), I(stake_log^2))

gen1<-GenMatch(Tr=Tr, X=X, BalanceMatrix=BalanceMatrix, pop.size=1000,exact=as.logical(c(0,0,0,0,0,1)))

mgen1<-Match(Y=Y, Tr=Tr, X=X, Weight.matrix=gen1,caliper=.8)

MatchBalance(Tr~A_ASSETS+A_AGE+contractdispute+home+stake_log+provincecode+I(A_ASSETS^2)+I(A_AGE^2)+I(A_ASSETS*A_AGE)+I(A_ASSETS*home)+I(A_AGE*home)+I(stake_log^2), match.out=mgen1, nboots=1000)

summary(mgen1)


######################################
#Non-SOEs
######################################

pribasic <- read.dta ("/Users/ywang/Documents/My Documents/Research/working papers/political connections and courts/data/working/CPS capture datafile/matching/PRImatch.dta")

attach(pribasic)
names(pribasic)

Y<-win
Tr<-treat

#Genetic Matching

X<-cbind(A_ASSETS, A_AGE, contractdispute, home, stake_log, provincecode)

BalanceMatrix<-cbind(A_ASSETS, A_AGE, contractdispute, home, stake_log, provincecode, I(A_ASSETS^2), I(A_AGE^2), I(A_ASSETS*A_AGE), I(A_ASSETS*home),I(A_AGE*home), I(stake_log^2))

gen2<-GenMatch(Tr=Tr, X=X, BalanceMatrix=BalanceMatrix, pop.size=1000, exact=as.logical(c(0,0,0,0,0,1)))

mgen2<-Match(Y=Y, Tr=Tr, X=X, Weight.matrix=gen2,caliper=.8)

MatchBalance(Tr~A_ASSETS+A_AGE+contractdispute+home+stake_log+provincecode+I(A_ASSETS^2)+I(A_AGE^2)+I(A_ASSETS*A_AGE)+I(A_ASSETS*home)+I(A_AGE*home)+I(stake_log^2), match.out=mgen2, nboots=1000)

summary(mgen2)

